package com.jp.knowledge.activity;

import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import com.jp.knowledge.R;
import com.jp.knowledge.comm.SlidingActivity;
import com.jp.knowledge.comm.JpApplication;

import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;

import com.google.gson.JsonObject;
import com.jiayen.util.ToasUtil;
import com.jp.knowledge.http.HttpHelper;
import com.jp.knowledge.model.IModel;
import com.jp.knowledge.util.ResultSubscriber;

/**
 *
 * 登录activity
 */
public class LoginSelectAcitivity extends SlidingActivity implements View.OnClickListener, ResultSubscriber.OnResultListener, JpApplication.SendMsgTimerCallBack {

    private EditText phone;
    private TextView smsCode;

    @Override
    protected int getResourceId() {
        return R.layout.activity_login;
    }

    @Override
    protected void init() {

        application.putLoginActivity(this);
        rightIcon.setVisibility(View.GONE);
        topDesc.setVisibility(View.GONE);
        topName.setText("登录");
        topName.setCompoundDrawables(null, null, null, null);
        leftIcon.setVisibility(View.VISIBLE);
        leftIcon.setImageResource(R.mipmap.arrow_white_left);
        leftIcon.setOnClickListener(this);

        phone = (EditText) findViewById(R.id.phone);
        smsCode= (TextView) findViewById(R.id.sms_code);
        smsCode.setOnClickListener(this);
        phone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {}
            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {}
            @Override
            public void afterTextChanged(Editable editable) {
                timerCallBack(application.getNextSendMsgTime());
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        application.setTimerCallBack(this);
        timerCallBack(application.getNextSendMsgTime());
    }

    private long preClickTime;      // 防止多次重复点击
    @Override
    public void onClick(View view) {
        long currentTime = System.currentTimeMillis();
        if(currentTime - preClickTime < 500){
            preClickTime = currentTime;
            return;
        }
        preClickTime = currentTime;

        if(view==smsCode){
            JsonObject param = new JsonObject();
            param.addProperty("phone",phone.getText().toString());
            HttpHelper.getInstance(mContext).getSmsCode(param,1,this);
        } else if(view == leftIcon){
            finish();
        }
    }

    @Override
    public void onStart(int requestType) {
        loading.show();
    }
    @Override
    public void onCompleted(int requestType) {
        loading.cancel();
    }
    @Override
    public void onError(int requestType) {
        ToasUtil.toast(mContext,"验证码发送失败，请稍后重试!");
        onCompleted(requestType);
    }

    @Override
    public void onNext(IModel data, int requestType) {
        if(data.getErrcode()==0){
            application.setNextSendMsgTime(60);
            timerCallBack(application.getNextSendMsgTime());
            application.startTimerCountdown();
            toCheckCode(phone.getText().toString());
        }else{
            ToasUtil.toast(mContext, data.getMessage());
        }
    }

    protected void toCheckCode(String phone){
        Intent intent =new Intent(mContext,LoginSecondActivity.class);
        intent.putExtra("phone",phone);
        startActivity(intent);
    }

    @Override
    public void timerCallBack(int time) {
        if(time > 0){
            smsCode.setEnabled(false);
            smsCode.setTextColor(getResources().getColor(R.color.gray_deep));
            smsCode.setText(String.format("获取验证码 (%d秒)", application.getNextSendMsgTime()));
        } else{
            if (phone != null && phone.getText().length() == 11) {
                smsCode.setEnabled(true);
                smsCode.setTextColor(getResources().getColor(R.color.theme_color));
            } else{
                smsCode.setEnabled(false);
                smsCode.setTextColor(getResources().getColor(R.color.gray_deep));
            }
            smsCode.setText("获取验证码");
        }
    }
}